package com.pactera.jep.service.sys.conf;

import com.pactera.jep.autoconfigure.core.conf.JepCoreProperties;
import com.pactera.jep.sys.dash.provider.spring.SpringDataProviderFactoryBean;
import com.pactera.jep.sys.mail.MailSender;
import com.pactera.jep.sys.mail.adapter.JavaMailSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableConfigurationProperties({JepSysProperties.class, JepCoreProperties.class})
@ConditionalOnProperty(prefix = "jep.sys", value = "enabled", matchIfMissing = true)
public class JepSysAutoConfiguration {
	
	@Autowired
	JepSysProperties jepSysProperties;

	@Autowired
	JepCoreProperties jepCoreProperties;

	@Autowired
	DataSource dataSource;

	@Bean
	@ConditionalOnMissingBean(SpringDataProviderFactoryBean.class)
	public SpringDataProviderFactoryBean getSpringDataProviderFactoryBean()
	{
		SpringDataProviderFactoryBean bean = new SpringDataProviderFactoryBean();
		bean.setTimeout(jepCoreProperties.getDashConfig().getTimeout());
		bean.setInterval(jepCoreProperties.getDashConfig().getInterval());
		return bean;
	}
	
	@Bean
	public MailSender getMailSender()
	{
		JavaMailSender mailSender = new JavaMailSender();
		mailSender.setUsername("ormtest");
		mailSender.setPassword("123456");
		mailSender.setMineType("text/html;charset=GBK");
		mailSender.setFrom("ormtest@163.com");
		Properties mailProps = new Properties();
		mailProps.setProperty("mail.smtp.host", "smtp.163.com");
		mailSender.setConfig(mailProps);
		return mailSender;
	}
}
